{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# OpenDataTools 支持港交所数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入hkex接口\n",
    "from opendatatools import hkex\n",
    "hkex.set_proxies({\"https\" : \"https://127.0.0.1:1080\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>date</th>\n",
       "      <th>name</th>\n",
       "      <th>percent</th>\n",
       "      <th>share_num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>平安银行</td>\n",
       "      <td>2.40%</td>\n",
       "      <td>412,874,639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>万  科Ａ</td>\n",
       "      <td>0.98%</td>\n",
       "      <td>96,153,353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000005.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>世纪星源</td>\n",
       "      <td>0.00%</td>\n",
       "      <td>2,833</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000006.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>深振业Ａ</td>\n",
       "      <td>0.02%</td>\n",
       "      <td>344,225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000008.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>神州高铁</td>\n",
       "      <td>0.14%</td>\n",
       "      <td>4,047,976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>中国宝安</td>\n",
       "      <td>0.21%</td>\n",
       "      <td>4,542,198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>000011.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>深物业A</td>\n",
       "      <td>0.04%</td>\n",
       "      <td>239,572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>000012.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>南  玻Ａ</td>\n",
       "      <td>0.08%</td>\n",
       "      <td>1,345,126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>000016.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>深康佳Ａ</td>\n",
       "      <td>0.37%</td>\n",
       "      <td>5,950,434</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>000018.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>神州长城</td>\n",
       "      <td>0.02%</td>\n",
       "      <td>383,833</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>000019.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>深深宝A</td>\n",
       "      <td>0.00%</td>\n",
       "      <td>2,610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>000021.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>深科技</td>\n",
       "      <td>0.04%</td>\n",
       "      <td>661,511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>000025.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>特力Ａ</td>\n",
       "      <td>0.04%</td>\n",
       "      <td>127,418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>000027.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>深圳能源</td>\n",
       "      <td>0.09%</td>\n",
       "      <td>3,603,006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>000028.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>国药一致</td>\n",
       "      <td>0.37%</td>\n",
       "      <td>1,389,754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>000030.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>富奥股份</td>\n",
       "      <td>0.07%</td>\n",
       "      <td>981,669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>000031.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>中粮地产</td>\n",
       "      <td>0.62%</td>\n",
       "      <td>11,271,202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>000034.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>神州数码</td>\n",
       "      <td>0.19%</td>\n",
       "      <td>1,302,116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>000035.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>中国天楹</td>\n",
       "      <td>0.04%</td>\n",
       "      <td>574,948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>000036.SZ</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>华联控股</td>\n",
       "      <td>0.04%</td>\n",
       "      <td>557,854</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         code        date   name percent    share_num\n",
       "0   000001.SZ  2018-05-28   平安银行   2.40%  412,874,639\n",
       "1   000002.SZ  2018-05-28  万  科Ａ   0.98%   96,153,353\n",
       "2   000005.SZ  2018-05-28   世纪星源   0.00%        2,833\n",
       "3   000006.SZ  2018-05-28   深振业Ａ   0.02%      344,225\n",
       "4   000008.SZ  2018-05-28   神州高铁   0.14%    4,047,976\n",
       "5   000009.SZ  2018-05-28   中国宝安   0.21%    4,542,198\n",
       "6   000011.SZ  2018-05-28   深物业A   0.04%      239,572\n",
       "7   000012.SZ  2018-05-28  南  玻Ａ   0.08%    1,345,126\n",
       "8   000016.SZ  2018-05-28   深康佳Ａ   0.37%    5,950,434\n",
       "9   000018.SZ  2018-05-28   神州长城   0.02%      383,833\n",
       "10  000019.SZ  2018-05-28   深深宝A   0.00%        2,610\n",
       "11  000021.SZ  2018-05-28    深科技   0.04%      661,511\n",
       "12  000025.SZ  2018-05-28    特力Ａ   0.04%      127,418\n",
       "13  000027.SZ  2018-05-28   深圳能源   0.09%    3,603,006\n",
       "14  000028.SZ  2018-05-28   国药一致   0.37%    1,389,754\n",
       "15  000030.SZ  2018-05-28   富奥股份   0.07%      981,669\n",
       "16  000031.SZ  2018-05-28   中粮地产   0.62%   11,271,202\n",
       "17  000034.SZ  2018-05-28   神州数码   0.19%    1,302,116\n",
       "18  000035.SZ  2018-05-28   中国天楹   0.04%      574,948\n",
       "19  000036.SZ  2018-05-28   华联控股   0.04%      557,854"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取陆港通 北向 持股 情况\n",
    "# 参数：市场SH/SZ，日期YYYY-MM-DD\n",
    "df = hkex.get_lgt_share(market = 'SZ', date = '2018-05-28')\n",
    "df.head(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>date</th>\n",
       "      <th>name</th>\n",
       "      <th>percent</th>\n",
       "      <th>share_num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>600000.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>浦发银行</td>\n",
       "      <td>0.67%</td>\n",
       "      <td>188,628,181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>600004.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>白云机场</td>\n",
       "      <td>12.97%</td>\n",
       "      <td>268,488,701</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>600006.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>东风汽车</td>\n",
       "      <td>0.02%</td>\n",
       "      <td>529,203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>600007.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>中国国贸</td>\n",
       "      <td>1.17%</td>\n",
       "      <td>11,840,812</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>600008.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>首创股份</td>\n",
       "      <td>1.03%</td>\n",
       "      <td>49,869,999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>600009.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>上海机场</td>\n",
       "      <td>31.30%</td>\n",
       "      <td>342,342,910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>600010.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>包钢股份</td>\n",
       "      <td>0.30%</td>\n",
       "      <td>66,405,460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>600011.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>华能国际</td>\n",
       "      <td>0.20%</td>\n",
       "      <td>21,171,052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>600012.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>皖通高速</td>\n",
       "      <td>0.30%</td>\n",
       "      <td>3,532,086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>600015.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>华夏银行</td>\n",
       "      <td>0.21%</td>\n",
       "      <td>27,377,361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>600016.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>民生银行</td>\n",
       "      <td>0.91%</td>\n",
       "      <td>271,846,795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>600017.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>日照港</td>\n",
       "      <td>1.21%</td>\n",
       "      <td>37,379,145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>600018.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>上港集团</td>\n",
       "      <td>0.18%</td>\n",
       "      <td>41,241,500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>600019.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>宝钢股份</td>\n",
       "      <td>2.35%</td>\n",
       "      <td>520,321,884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>600020.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>中原高速</td>\n",
       "      <td>0.27%</td>\n",
       "      <td>6,130,940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>600021.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>上海电力</td>\n",
       "      <td>0.14%</td>\n",
       "      <td>3,136,033</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>600023.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>浙能电力</td>\n",
       "      <td>0.20%</td>\n",
       "      <td>27,948,524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>600026.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>中远海能</td>\n",
       "      <td>0.25%</td>\n",
       "      <td>6,984,476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>600027.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>华电国际</td>\n",
       "      <td>0.19%</td>\n",
       "      <td>13,086,134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>600028.SH</td>\n",
       "      <td>2018-05-28</td>\n",
       "      <td>中国石化</td>\n",
       "      <td>0.56%</td>\n",
       "      <td>543,596,346</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         code        date  name percent    share_num\n",
       "0   600000.SH  2018-05-28  浦发银行   0.67%  188,628,181\n",
       "1   600004.SH  2018-05-28  白云机场  12.97%  268,488,701\n",
       "2   600006.SH  2018-05-28  东风汽车   0.02%      529,203\n",
       "3   600007.SH  2018-05-28  中国国贸   1.17%   11,840,812\n",
       "4   600008.SH  2018-05-28  首创股份   1.03%   49,869,999\n",
       "5   600009.SH  2018-05-28  上海机场  31.30%  342,342,910\n",
       "6   600010.SH  2018-05-28  包钢股份   0.30%   66,405,460\n",
       "7   600011.SH  2018-05-28  华能国际   0.20%   21,171,052\n",
       "8   600012.SH  2018-05-28  皖通高速   0.30%    3,532,086\n",
       "9   600015.SH  2018-05-28  华夏银行   0.21%   27,377,361\n",
       "10  600016.SH  2018-05-28  民生银行   0.91%  271,846,795\n",
       "11  600017.SH  2018-05-28   日照港   1.21%   37,379,145\n",
       "12  600018.SH  2018-05-28  上港集团   0.18%   41,241,500\n",
       "13  600019.SH  2018-05-28  宝钢股份   2.35%  520,321,884\n",
       "14  600020.SH  2018-05-28  中原高速   0.27%    6,130,940\n",
       "15  600021.SH  2018-05-28  上海电力   0.14%    3,136,033\n",
       "16  600023.SH  2018-05-28  浙能电力   0.20%   27,948,524\n",
       "17  600026.SH  2018-05-28  中远海能   0.25%    6,984,476\n",
       "18  600027.SH  2018-05-28  华电国际   0.19%   13,086,134\n",
       "19  600028.SH  2018-05-28  中国石化   0.56%  543,596,346"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取陆港通 北向 持股 情况\n",
    "# 参数：市场SH/SZ，日期YYYY-MM-DD\n",
    "df = hkex.get_lgt_share(market = 'SH', date = '2018-05-28')\n",
    "df.head(20)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
